/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.gui.security;

import it.polimi.registration.business.security.boundary.UserManager;
import it.polimi.registration.business.security.entity.User;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author miglie
 */
@Named
@RequestScoped
public class RegistrationBean {
    
    @Inject
    private Logger logger;

	@EJB
	private UserManager um;

	private User user;

	public RegistrationBean() {
	}

	public User getUser() {
		if (user == null) {
			user = new User();
		}
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String register() {
        FacesContext context = FacesContext.getCurrentInstance();
        
		try {
			um.save(user);
			return "/index.xhtml?faces-redirect=true";
		} catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Registering Failed","Registering Failed"));
            logger.log(Level.SEVERE,"Register Failed");
            return null;
		}
	}

}
